var $ = require("../../utils/Ajax.js");
Page({
  data: {
    liveVideoNext: null,
    Title: null,
    VideoId: null,
    LiveVideoNextId: null,

    // 图片路径配置
    PictureUrl: getApp().PictureUrl,

    // 判断是否有直播
    TopLive: false,

    // 是否自动播放
    autoplay: false,

    // 是否显示默认播放控件
    controls: false,

    // 指定视频时长
    duration: 6,

    // 首页咨询数据
    Advisory: [],

    // 精彩回放数据
    PlayBack: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options){
    wx.setNavigationBarTitle({
      title: "君哥有话"
    }) 
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        $.Ajax("api/userLocaltion", "POST", {
          latitude: res.latitude,
          longitude: res.longitude
        }, function (res){})
      }
    })
  },

  onShow: function () {
    var that = this;
    $.Ajax("api/index", "GET", {}, function (res) {
      console.log(res)
      if (res.s) {
        for (var i = 0; i < res.d.information.data.length; i++) {
          res.d.information.data[i].coverNum = res.d.information.data[i].cover.length
        }
        if (res.d.liveVideo == null) {
          that.setData({
            Title: '暂无直播,请稍后再试',
            liveVideoNext: res.d.liveVideoNext,
            VideoId: null,
            LiveVideoNextId: null,
            TopLive: false
          })
          $.Ajax("api/video", "GET", {}, function (res) {
            if (res.s) {
              that.setData({
                PlayBack: res.d.videos.data
              })
            } else {
              wx.showToast({
                title: res.m,
                icon: 'none',
                duration: 3000,
                mask: true
              })
            }
          });
        } else {
          that.setData({
            Title: res.d.liveVideo.title,
            Config: res.d.config,
            VideoId: res.d.liveVideo.id,
            liveVideoNext: res.d.liveVideoNext,
            LiveVideoNextId: res.d.liveVideo.liveVideoNextId,
            TopLive: true
          })
        }
        that.setData({
          Advisory: res.d.information.data
        })
      } else {
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 3000,
          mask: true
        })
      }
    })
  },

  // 页面触底处理函数
  ScrollPostlower: function () {
    console.log("这里处理用户下拉加载更多咨询消息");
  },

  // 点击观看直播
  Live: function(e){
    var VideoId = e.currentTarget.dataset.videoid;
    var LiveVideoNextId = e.currentTarget.dataset.livevideonextid;
    if (VideoId == null || VideoId == undefined){
      wx.showToast({
        title: '暂无直播,请稍后再试',
        icon: 'none',
        duration: 3000,
        mask: true
      })
    }else{
      wx.navigateTo({ 
        url: '../Live/Live?VideoId=' + VideoId + '&LiveVideoNextId=' + LiveVideoNextId,
      })
    }
  },

  // 点击下期预告
  ClickTopic: function(){
    var that = this;
    $.Ajax("api/video","GET",{},function(res){
      that.setData({
        TopLive: false
      })
      if (res.s){
        console.log(res)
        that.setData({
          PlayBack: res.d.videos.data
        })
      }else{
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 3000,
          mask: true
        })
      }
    });
  },

  liveVideoNext: function (e) {
    var that = this;
    var Tivket = e.currentTarget.dataset.ticket;
    var Id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '门票购买',
      content: '你确定要购买下期直播门票吗?',
      confirmText: "是",
      cancelText: "否",
      success: function (res) {
        if (res.confirm) {
          // 点击是
          $.Ajax("api/ticket", "POST", {
            id: Number(Id),
            t: 1
          }, function (res) {
            wx.showToast({
              title: res.m,
              icon: 'none',
              duration: 1500,
              mask: true
            })
            if (res.m == "余额不足"){
              setTimeout(function () {
                wx.navigateTo({
                  url: '../TopUpBeans/TopUpBeans',
                })
              }, 2000)
            }
          })
        }
      }
    });
  },

  // 点击返回直播
  ClickTopic_: function(){
    this.setData({
      TopLive: true
    })
  },

  // 点击精彩回放
  PlayBack: function(e){
    var Token = wx.getStorageSync('Token');
    var UserInfor = wx.getStorageSync('UserInfor');
    if (Token == '' || Token == null) {
      wx.removeStorageSync('Token');
      wx.removeStorageSync('UserInfor');
      wx.switchTab({
        url: '../My/My',
      })
    }else{
      var Id = e.currentTarget.dataset.id;
      var Type = e.currentTarget.dataset.type;
      wx.navigateTo({
        url: '../PlayBack/PlayBack?Id=' + Id + "&Type=" + Type
      })
    }
  },

  FromId: function (e) {
    $.Ajax("api/addFromId", "GET", {
      fromId: e.detail.formId
    }, function (res) {
      console.log(res)
    })
  }
})